package com.turtle3d.user.visualizators;

import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.vecmath.Color3f;
import javax.vecmath.Matrix4f;

import com.turtle3d.ResourceManager;
import com.turtle3d.formallanguage.VisualSymbol;
import com.turtle3d.formallanguage.symbol.Symbol;
import com.turtle3d.visualizator.VisualData;
import com.turtle3d.visualizator.Visualizator;


public class Ball extends Visualizator {

	/**
	 * @param args
	 */
	
	public BranchGroup visualize(VisualData visualData){
		BranchGroup branchGroup = new BranchGroup();
		
		ArrayList<VisualSymbol> visualSequence = visualData.getVisualSequence();
				
		for(int i=0;i<visualSequence.size();i++){
			
			VisualSymbol visualSymbol = visualSequence.get(i);
			
			if(!visualSymbol.getSymbol().equals(new Symbol("F")))
				continue;
			
			int depth = visualSymbol.getDepth();															
			//Float d = visualSymbol.getSymbol().getParameter(Symbol.DISTANCE);
			float normalizedDepth = depth/(visualData.getMaximalDepthReached()*1f);
			Float r = visualSymbol.getSymbol().getParameter(Symbol.RADIUS);
			
			Shape3D ball = ResourceManager.getShape3D("ball");
			
			Matrix4f scale = new Matrix4f();
			scale.setIdentity();
			float grow = visualSymbol.getSymbol().getParameter("grow");
			scale.setScale(r+r*grow*normalizedDepth*normalizedDepth);
			Matrix4f transform = visualSymbol.getTransform();
			transform.mul(scale);

			ball.setName("Grammar_"+depth);

			Appearance appearance = new Appearance();
			Material m = new Material();					
			m.setDiffuseColor(getRainbowColor(normalizedDepth));
			appearance.setMaterial(m);
			ball.setAppearance(appearance);						
			
			branchGroup.addChild(createTransformGroup(transform, ball));
			
		}					
		
		return branchGroup;
	}
	
	public void setRequiredParameters(){
		setRequiredParamter("grow", 1f);
	}

}
